﻿Imports MyGeneration.dOOdads

Public Class controller_vendedores

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(ByVal value As Exception)
            _MessageError = value
        End Set
    End Property

#Region "Vendedores"

    Public Function Insert_Vendedor(ByVal conn As String, ByVal vendedor As String, ByVal documento As String, _
                                    ByVal usuario As String, ByVal password As String, _
                                    ByVal codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim pro As New ClassSigeco.ClassSigeco.si_vendedor(conn)

            pro.AddNew()
            pro.Vendedor = vendedor
            pro.Documento = documento
            pro.Usuario = usuario
            pro.Contrasena = password
            pro.Habilitado = True
            pro.ActualizadoPor = codUsuario
            pro.ActualizadoFecha = DateTime.Now
            pro.Save()

            tx.CommitTransaction()

            Return pro.IdVendedor

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Vendedor(ByVal conn As String, ByVal idVendedor As Integer, ByVal vendedor As String, ByVal documento As String, _
                                    ByVal usuario As String, ByVal password As String, _
                                    ByVal codUsuario As Integer, ByVal habilitado As Boolean) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim pro As New ClassSigeco.ClassSigeco.si_vendedor(conn)
            pro.Where.IdVendedor.Value = idVendedor

            If pro.Query.Load Then

                pro.Vendedor = vendedor
                pro.Documento = documento
                pro.Usuario = usuario
                pro.Contrasena = password
                pro.Habilitado = habilitado
                pro.ActualizadoPor = codUsuario
                pro.ActualizadoFecha = DateTime.Now
                pro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Vendedor(ByVal conn As String, ByVal idVendedor As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim pro As New ClassSigeco.ClassSigeco.si_vendedor(conn)
            pro.Where.IdVendedor.Value = idVendedor

            If pro.Query.Load Then

                pro.MarkAsDeleted()
                pro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Vendedores_All(ByVal conn As String, ByVal idVendedor As Integer, ByVal vendedor As String, _
                                        ByVal documento As String, ByVal habilitados As Boolean) As DataTable
        Try

            Dim vPro As New ClassSigeco.ClassSigeco.vw_vendedores(conn)

            If idVendedor > 0 Then
                vPro.Where.IdVendedor.Value = idVendedor
            End If

            If vendedor <> "" Then
                vPro.Where.Vendedor.Value = "%" & vendedor & "%"
                vPro.Where.Vendedor.Operator = WhereParameter.Operand.Like
            End If

            If documento <> "" Then
                vPro.Where.Documento.Value = "%" & documento & "%"
                vPro.Where.Documento.Operator = WhereParameter.Operand.Like
            End If

            If habilitados Then
                vPro.Where.Habilitado.Value = habilitados
            End If

            vPro.Query.AddOrderBy("vendedor", WhereParameter.Dir.ASC)

            If vPro.Query.Load Then
                If vPro.DefaultView.Table.Rows.Count > 0 Then
                    Return vPro.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region


End Class
